package com.aaa.medicine.controller;

import com.aaa.entity.InventoryLog;
import com.aaa.entity.Medicines;
import com.aaa.entity.Producer;
import com.aaa.entity.PurchaseItem;
import com.aaa.medicine.service.IInventoryLogService;
import com.aaa.medicine.service.IMedicinesService;
import com.aaa.medicine.service.IProducerService;
import com.aaa.medicine.service.impl.InventoryServiceImpl;
import com.aaa.medicine.vo.InventoryLogVo;
import com.aaa.medicine.vo.MedicinesVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("medicine/Inventory/")
public class InventoryController {
    /**
     * 药品总库存
     */
    @Autowired
    private IMedicinesService iMedicinesService;
    @PostMapping("getAll2")
    public Result<Page<Medicines>> getAll1(@RequestBody MedicinesVo medicines){
        QueryWrapper<Medicines> queryWrapper = new QueryWrapper();
        Page objectPage = new Page(medicines.getCurrent(),medicines.getSize());
        if (StringUtils.isNotBlank(medicines.getProducterId())){
            queryWrapper.like("producter_id",medicines.getProducterId());
        }
        if (StringUtils.isNotBlank(medicines.getKeywords())){
            queryWrapper.like("keywords",medicines.getKeywords());
        }
        if (StringUtils.isNotBlank(medicines.getMedicinesType())){
            queryWrapper.like("medicines_type",medicines.getMedicinesType());
        }
        if (StringUtils.isNotBlank(medicines.getMedicinesName())){
            queryWrapper.like("medicines_name",medicines.getMedicinesName());
        }
        if (StringUtils.isNotBlank(medicines.getPrescriptionType())){
            queryWrapper.like("prescription_type",medicines.getPrescriptionType());
        }
        if(Objects.nonNull(medicines.getDateRange())&&medicines.getDateRange().length==2){
            queryWrapper.between("create_time",medicines.getDateRange()[0],medicines.getDateRange()[1]);
        }
        IPage<Medicines> page2 = iMedicinesService.page(objectPage,queryWrapper);
        return new Result(page2);
    }

    /**
     * 批次库存及价格
     */
    @Resource
    private IInventoryLogService iInventoryLogService;
    @PostMapping("getAll")
    public Result<IPage<InventoryLog>> getAll(Page page,InventoryLogVo inventoryLogVo){
       QueryWrapper<InventoryLog> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(inventoryLogVo.getPurchaseId())){
            wrapper.like("purchase_id",inventoryLogVo.getPurchaseId());
        }
        if (StringUtils.isNotBlank(inventoryLogVo.getMedicinesName())){
            wrapper.like("Medicines_name",inventoryLogVo.getMedicinesName());
        }
        if (StringUtils.isNotBlank(inventoryLogVo.getMedicinesType())){
            wrapper.like("Medicines_type",inventoryLogVo.getMedicinesType());
        }
        if (StringUtils.isNotBlank(inventoryLogVo.getProducterId())){
            wrapper.like("producter_id",inventoryLogVo.getProducterId());
        }
        if (StringUtils.isNotBlank(inventoryLogVo.getPrescriptionType())){
            wrapper.like("prescription_type",inventoryLogVo.getPrescriptionType());
        }
        if(Objects.nonNull(inventoryLogVo.getDateRange())&&inventoryLogVo.getDateRange().length==2){
            wrapper.between("create_time",inventoryLogVo.getDateRange()[0],inventoryLogVo.getDateRange()[1]);
        }
        Page<InventoryLog> page1 = iInventoryLogService.page(page,wrapper);
        return new Result(page1);
    }

    /**
     * 生产厂家下拉框
     */
    @Autowired
    private IProducerService iProducerService;
    @GetMapping("getAll1")
    public Result<List<Producer>> getAll1(){
        return new Result(iProducerService.list());
    }
/**
 * 查询所有药品
 */
@Autowired
private InventoryServiceImpl inventoryService;
@PostMapping("getAll3")
    public Result<List<PurchaseItem>> getAll3(){
    return new Result(inventoryService.list());
}
@PostMapping("getAll4/{puchaseId}")
    public Result<List<PurchaseItem>> getAll4(@PathVariable String puchaseId){
    return new Result(inventoryService.getAll4(puchaseId));
}
}
